var app = getApp()
Page({
    data: {
        skipToPartnerIndex: false,
        canSkipToApplyDetail: false,
        userListInfo: [{
            icon: '../../images/iconfont-dingdan.png',
            text: '提交订单',
            url: '../submitOrder/index'
        }, {
            icon: '../../images/iconfont-dingdan.png',
            text: '我的订单',
            url: '../myorder/index'
        },
        {
            icon: '../../images/iconfont-card.png',
            text: '我的伙伴',
            url: '../partner/index'
        }, {
            icon: '../../images/iconfont-grade.png',
            text: '我的等级',
            url: '../grade/index'
        }, {
            icon: '../../images/iconfont-icontuan.png',
            text: '预估收入',
            url: '../estimate-income/index'
        }, {
            icon: '../../images/footer-icon-03.png',
            text: '结算中心',
            url: '../caculate-credit/index'
        }]
        ,
        credit1: "0.00",
        credit2: "0.00",
        credit3: "0.00",
        type: 0,
        pid: "",
        invitepeople: "无",
        qrcode: "",
        memberid: 0,
    },
    

    openwincommission: function (e) {
        wx.setStorage({
            key: "sendType",
            data: e.currentTarget.dataset.type
        });
        wx.navigateTo({
            url: '../commission/index'
        })



    },
    onShow: function () {
        // Do something when page show.
      
    },
    onLoad: function () {
        var that = this
        var globalset = wx.getStorageSync("globalset");
        that.setData({"applygradename":globalset.applygradename});
        
       
        var memberinfo = wx.getStorageSync("memberinfo");
        // //获取缓存判定用户类型
       
        if(memberinfo.type==2){
         
            wx.setStorage({
                key: "ispartner",
                data: true
            });
            wx.reLaunch({
                url: '../index/index?partner=1',
            })
        }
        that.getUserInfor();
        that.getMember();


        //调用应用实例的方法获取全局数据
        app.getUserInfo(function (userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            })
        })
        wx.showShareMenu({
            withShareTicket: true
        })


        wx.getStorage({
            key: "memberinfo",
            success: function (res) {
              
                that.setData({
                    memberid: res.data.memberid,
                    invitepeople: res.data.from_nickname,
                    type: res.data.type
                });
            }
        });



        //取会员的globalset
        wx.getStorage({
            key: 'globalset',
            success: function (res) {
                
                that.setData({
                    globalset: res.data
                });

               
                if (that.data.type != 0 || that.data.globalset.isopen_subsidy != 1) {
                    var newuserListInfo = that.data.userListInfo;
                    newuserListInfo.splice(0, 1);
                   
                    that.setData({ userListInfo: newuserListInfo })
                }
            },
        });
        that.showRecommedCode();

        //  that.getMyApplayPartner();

    },
 
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
       
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    getUserInfor: function () {
        var $that = this;
       
        app.util.request({
            url: 'entry/wxapp/AddMember',
            showLoading: false,
            data: {
                "m": 'nets_haojk',
                // "from_uid": e.data
                "from_uid": "0"
            },
            method: 'post',
            success: function (response) {
                
                wx.setStorage({
                    key: "myUserinformation",
                    data: response.data.data
                });

               
                var obj = response.data.data;
                $that.setData({
                    credit1: obj.credit1 == null ? 0 : obj.credit1,
                    credit2: obj.credit2,
                    type: obj.type,
                    pid: obj.pid
                })
            }
        });
    },

    //顶部分享
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
           
        }
       
        return {
            title: this.data.globalset.title,
            path: '/haojk/pages/index/index?from_uid=' + this.data.memberid,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }

    },
    applyvip: function () {
        var $that = this;


        app.util.request({
            url: 'entry/wxapp/CheckApplyleader',
            data: {
                "m": 'nets_haojk'
            },
            method: 'post',
            success: function (response) {
              
            
                if(response.data.data.code==200){
                    app.util.request({
                        url: 'entry/wxapp/Applyleader',
                        data: {
                            "m": 'nets_haojk'
                        },
                        method: 'post',
                        success: function (response) {

                           
                            var obj = response.data;
                            

                            wx.setStorageSync("applyleader_fee", obj.data.applyleader_fee);
                           
                            if (obj.data.code == 300) {
                                wx.navigateTo({
                                    url: "../apply-vip/index"
                                });
                            } else {
                                $that.setData({
                                    'type': 1
                                });
                                //首页存储会员对象到缓存，其他页面直接读取缓存即可
                                wx.setStorage({
                                    key: "memberinfo",
                                    data: response.data.data
                                });
                                $that.setData({
                                    memberid: response.data.data.memberid,
                                    datalock: false
                                });
                                wx.showModal({
                                    title: '提示',
                                    content: response.data.message,//'您可以通过转发、生成二维码的方式，邀请好友进入小程序网购赚佣金',
                                    success: function (res) {
                                        if (res.confirm) {
                                           
                                            //wx.reLaunch({
                                            //  url: "../my/index"
                                            //});
                                        } else if (res.cancel) {
                                            //wx.reLaunch({
                                            //  url: "../my/index"
                                            //});
                                        }
                                    }
                                });
                            }
                        }
                    });
                } else if (response.data.data.code==500){
                    wx.showModal({
                        title: '提示',
                        content: response.data.data.msg,
                    })
                 }
                
            }
        });
        
        /*
        wx.navigateTo({
          url: "../apply-vip/index"
        })
        */
    }
    , getMember: function () {

        var that = this;
        app.getUserInfo(function (userInfo) {
            
            
            //更新数据
            app.util.getUserInfo(function (response) {

                response.memberInfo.nickname = userInfo.nickName;
                response.memberInfo.avatar = userInfo.avatarUrl;
                response.memberInfo.city = userInfo.city;
                response.memberInfo.country = userInfo.country;
                response.memberInfo.province = userInfo.province;
                response.memberInfo.gender = userInfo.gender;
                that.addMember(response.memberInfo);
            });
        });
        // 页面渲染完成
    },
    addMember: function (userInfo) {
        var $this = this;
        app.util.request({
            url: 'entry/wxapp/AddMember',
            showLoading: false,
            data: {
                "m": 'nets_haojk',
                "from_uid": this.data.from_uid,
                "nickname": userInfo.nickname,
                "avatar": userInfo.avatar,
                "city": userInfo.city,
                "country": userInfo.country,
                "province": userInfo.province,
                "gender": userInfo.gender,
            },
            method: 'post',
            success: function (response) {
              
                //首页存储会员对象到缓存，其他页面直接读取缓存即可
                wx.setStorageSync("memberinfo", response.data.data);

                //取会员的globalset
                wx.getStorage({
                    key: 'memberinfo',
                    success: function (res) {
                      
                        $this.setData({
                            type: res.data.type,
                            level: res.data.level,
                            mobile: res.data.mobile
                        });
                    },
                });





            }
        });
    },
    browseShop: function () {
        wx.switchTab({
            url: '../index/index',
            success: function (e) {
                var page = getCurrentPages().pop();
              
                if (page == undefined || page == null) return;
                page.onLoad();
                
            }
        })
    },
    //推荐二维码的显示
    showRecommedCode: function () {
        var $this = this;
        app.util.request({
            url: 'entry/wxapp/Getwxacodeunlimit',
            data: {
                "m": 'nets_haojk'
            },
            method: 'post',
            success: function (response) {
                $this.setData({
                    qrcode: response.data.data.path,
                    datalock: false,
                    imgpath: response.data.data.path + "?time=" + new Date().getMilliseconds()

                });

             
            }
        });
    },
    showRecommedCode1: function () {
        var $that = this;
        if ($that.data.imgpath) {
            var imgpath = $that.data.imgpath;
            wx.previewImage({
                current: imgpath,
                urls: [imgpath],//内部的地址为绝对路径
                fail: function () {
                  
                },
                complete: function () {
                    console.info("点击图片了");
                    /*wx.navigateBack({
                      delta: 2
                    })*/
                },
            })
        }

    },
  
    skipToPartnerDetail: function () {

        var $that = this;
        wx.navigateTo({
            url: '../partnerApply/index',

        })

       
    }



})